package cn.bocweb.spinning.model.Factory;


import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;


/**
 * Created by kaizen on 15/11/14.
 */
public class ApiHeaders implements Interceptor {

    public static String sessionId;

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public static void clearSessionId() {
        sessionId = null;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (sessionId != null) {
            Request sessionRequest = request
                    .newBuilder()
                    .addHeader("Cookie", sessionId)
                    .build();
            return chain.proceed(sessionRequest);
        }
        Response response = chain.proceed(request);
        sessionId = response.headers().get("Set-Cookie");
        return response;
    }
}
